package src;

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.lyfam.game.behaviosr.steering.FleeingBehavior;
import com.lyfam.game.behaviosr.steering.ISteeringBehavior;
import com.lyfam.game.behaviosr.steering.SeekingBehavior;
import com.lyfam.game.behaviosr.steering.SteeringEngine;
import com.lyfam.game.behaviosr.steering.WanderingBehavior;
import com.lyfam.game.entity.Obstacle;
import com.lyfam.game.entity.Vehicle;
import com.lyfam.game.geometry.Vector2D;

import processing.core.PApplet;

@SuppressWarnings("serial")
public class SteeringTestWandering extends PApplet
{
	private static final int NUM_VEHICLES = 20;
	List<Vehicle> vehicles = new ArrayList<Vehicle>(10);
	ISteeringBehavior seek;
	Vector2D target = new Vector2D(200, 200);
	
    public void setup()
    {
    	for (int i = 0; i < NUM_VEHICLES; i++)
    	{
    		Vector2D v = new Vector2D(width/2, height/2);
    		Vehicle veh = new Vehicle(v, new Vector2D(random(-1,1), random(-1,1)), random(2, 7.5f), random(0.5f, 2.0f));
    		SteeringEngine engine = new SteeringEngine();
    		veh.setMass(random(2, 5));
    		if (i == 0)
    		{
    			seek = new WanderingBehavior(veh, g);
    			((WanderingBehavior) seek).setShowWanderingCircle(false);
    		}
    		else
    		{
    			seek = new FleeingBehavior(veh, vehicles.get(0).getPosition());
    			engine.addBehavior(new WanderingBehavior(veh, g, random(10, 40), random(20, 70), random(0.05f, 4.5f)));
    		}
    		engine.addBehavior(seek);
    		veh.setSteeringengine(engine);
    		veh.setMass(1.05f);
    		vehicles.add(veh);
    	}
    	
        setSize(400, 400);
        colorMode(RGB, 100);

        smooth();
        frameRate(60);
    }
    
    public void draw()
    {
    	background(100);
    	Obstacle obs = new Obstacle(target);
		obs.render(g, 2);
		
		Iterator<Vehicle> it = vehicles.iterator();
		int i = 0;
		while (it.hasNext())
		{
			Vehicle veh = it.next();
			if (i == 0)
			{
			    fill(204, 102, 0);
			    veh.setSize(10);
			    veh.render(g);
				veh.update();
			}
			else
			{
				if (veh.getPosition().distance(vehicles.get(0).getPosition()) < 5)
				{
					//vehicles.remove(i);
				}
				else
				{
					veh.render(g);
					veh.update();					
				}
			}
			fill(200);
			i++;
		}
		System.out.println(vehicles.size());
    }

	@Override
	public void mouseClicked(MouseEvent arg0)
	{
		target.setX(arg0.getX());
		target.setY(arg0.getY());
	}
    
}
